package database;

import Entity.Department;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 *@author BinhNV_A04911
 */
public class DepartmentManagerDB {

    private CallableStatement cstmt;
    private java.sql.Connection conn;
    private ResultSet rs;

    public DepartmentManagerDB(java.sql.Connection conn) {
        this.conn = conn;
    }
    
    public void loadData(DepartmentManagerDB departmentManager) {
        try {
            cstmt = conn.prepareCall("{call sp_GetDepartment}");
            rs =  cstmt.executeQuery();
            while(rs.next()) {
              
                int departmentID = rs.getInt("DepartmentID");
                String departmentName = rs.getString("DepartmentName");
                boolean status = rs.getBoolean("Status");
                Department obj = new Department(departmentID, departmentName, status);
                departmentManager.add(obj);
            }
            rs.close();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(DepartmentManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void deleteDepartment(Department obj) {
        try {
            cstmt = conn.prepareCall("{call sp_DeleteDepartment(?)}");
            cstmt.setInt(1, obj.getDepartmentID());
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(DepartmentManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void insertDepartment(Department obj) {
        try {
            cstmt = conn.prepareCall("{call sp_InsertDepartment(?,?,?)}");
            cstmt.setInt(1, obj.getDepartmentID());
            cstmt.setString(2, obj.getDepartmentName());
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(DepartmentManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void updateDepartment(Department obj) {
        try {
            cstmt = conn.prepareCall("{call sp_UpdateDepartment(?,?,?)}");
           
            cstmt.setInt(1, obj.getDepartmentID());
            cstmt.setString(2, obj.getDepartmentName());
            cstmt.setBoolean(3, obj.isStatus());
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(DepartmentManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
